package com.changgou.goods.controller;
import com.changgou.goods.pojo.Brand;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@CrossOrigin//这一个注解就表示允许跨域操作,因为他可以在请求和响应的头信息上加上access-control-allow-origin
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("/findAll")
    public Result findAll(){
        List<Brand> brands = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",brands);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")Integer id){
        Brand brand=brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",brand);
    }

    @PostMapping   //这里不在后面加上/add是因为用了restful风格
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"新增成功");
    }

    @PutMapping("/{id}")        /*这里的参数就是考虑怎么传递的问题*/
    public Result update(@PathVariable("id") Integer id,@RequestBody Brand brand){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        brandService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    //根据条件查询
    @GetMapping("/search")      //这里应该是@RequestParam,表示在请求地址栏后面跟的参数可以拿到,用@RequestBody的话必须穿过来的是消息体中的数据
    public Result search(@RequestParam Map<String,Object> searchmap){
        List<Brand> brands=brandService.search(searchmap);
        return new Result(true,StatusCode.OK,"根据条件查询成功",brands);
    }

    //分页查询
    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@PathVariable("page") Integer page ,@PathVariable("size")Integer size){
        PageResult result = brandService.findPage(page, size);
        return new Result(true,StatusCode.OK,"分页查询成功",result);
    }

    //根据条件分页查询
    @GetMapping("/fc/{page}/{size}")   //地址栏这样写可以拿到参数http://localhost:9011/brand/fc/2/2?letter=C
    public Result findByCondition(@RequestParam Map<String,Object> map,@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        PageResult result=brandService.findByCondition(map,page,size);
        int i=1/0;
        return new Result(true,StatusCode.OK,"根据条件分页查询成功",result);
    }

    //根据分类名称查询品牌指定字段列表(涉及三张表tb_brand，tb_category,tb_brand_category)多对多关系，通过中间关系表连接
    @GetMapping("/category/{categoryName}")
    public Result findListByCategoryName(@PathVariable("categoryName")String categoryName){

        List<Map> listByCategoryName = brandService.findListByCategoryName(categoryName);
        return new Result(true,StatusCode.OK,"根据分类名查询品牌成功",listByCategoryName);
    }
}
